package com.secneo.appshare.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.secneo.appshare.R;
import com.secneo.appshare.api.RemoteApi.AppInfoUser;

public class CommunityFeedsAdapter extends BaseAdapter{

	private List<AppInfoUser> mList;
	private Context ctx;
	
	public CommunityFeedsAdapter(Context ctx, List<AppInfoUser> list) {
		this.ctx = ctx;
		this.mList = list;
	}
	
	public void freeDrawable()
	{
		for(int i=0; i<mList.size(); i++)
		{
			mList.get(i).icon_drawable = null;
		}
		//mList.clear();
		System.gc();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		FeedItem feedItem;
		AppInfoUser feed = mList.get(position);
		if (convertView == null) {
			View v = LayoutInflater.from(ctx).inflate(R.layout.appshare_app_item, null);
			
			feedItem = new FeedItem();
			feedItem.mAppIcon = (ImageView)v.findViewById(R.id.ivAppIcon);
			feedItem.mAppName = (TextView)v.findViewById(R.id.tvAppName);
			
			v.setTag(feedItem);
			convertView = v;
		} else {
			feedItem = (FeedItem)convertView.getTag();
		}
		// set icon
		if (feed.icon_drawable == null) {
			feedItem.mAppIcon.setImageResource(R.drawable.some_user);
		} else {
			feedItem.mAppIcon.setImageDrawable(feed.icon_drawable);
		}
		
		// set name
		feedItem.mAppName.setText(feed.uname);
		
		return convertView;
	}

	class FeedItem {
		ImageView mAppIcon;
		TextView mAppName;
	}
}
